import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Fav from '../views/Fav.vue'
import News from '../views/News.vue'
import User from '../views/User.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {path: '/',name: 'Home',component: Home},
  {path: '/fav', component: Fav},
  {path: '/news', component: News},
  {
	  path: '/me', 
	  component: User,
	  beforeEnter:(to,from,next)=>{
	  	//如果用户访问是登陆界面，则是需要获取客户端保存的token值，有则放行，没有则跳转登陆
	  	const tokenStr=window.sessionStorage.getItem('token')
	  	if(!tokenStr) return next('/login')
		next()
	  }
  },
  {
	  path: '/login', 
	  component: Login
	}
]

const router = new VueRouter({
  routes
})

// //可以利用vue-router的导航钩子实现登陆与否的判断：未登录则跳转到登陆界面
// router.beforeEach((to,from,next)=>{
// 	//如果访问的是登陆界面，直接放行
// 	if(to.path==='/login') return next()
// 	//如果用户访问的不是登陆界面，则是需要获取客户端保存的token值，有则放行，没有则跳转到登陆界面
// 	const tokenStr=window.sessionStorage.getItem('token')
// 	if(!tokenStr) return next('/login')
// 	next()
// })

export default router
